<template>
    <div class="app-container">
        <el-card class="box-card">
            
            <div class="flex justify-between">
                <div class="flex justify-end">
                <div class="flex items-center mr-4">
                    <span class="item">用户编号：</span>
                    <el-input v-model="listQuery.user_code" clearable placeholder="请输入用户编号" size="normal"></el-input>
                </div>
                <div class="flex items-center mr-4">
                    <span class="item">用户昵称：</span>
                    <el-input v-model="listQuery.nick_name" clearable placeholder="请输入用户昵称" size="normal"></el-input>
                </div>
                <div class="flex items-center mr-4">
                    <span class="item">手机号码：</span>
                    <el-input v-model="listQuery.mobile" clearable placeholder="请输入用户手机号码" size="normal"></el-input>
                </div>
                <el-button size="default" type="primary" @click="getTableData">搜索</el-button>
                <el-button size="default" type="primary" @click="handleReset">重置</el-button>
                </div>
            </div>
            <el-table
                ref="multipleTable"
                v-loading="loading"
                :data="tableData"
                border
                class="mt-3"
                stripe
                style="width: 100%"
                tooltip-effect="dark"
            >
                <el-table-column :index="indexMethod" label="序号" prop="user_code" type="index" width="100px"></el-table-column>
                <el-table-column label="萌宠ID" prop="id"></el-table-column>
                <el-table-column label="萌宠" prop="name"></el-table-column>
                <el-table-column label="萌宠品质" prop="quality"></el-table-column>
                <el-table-column label="萌宠等级" prop="level"></el-table-column>
                <el-table-column label="洗练次数" prop="refresh_property_count"></el-table-column>
                <el-table-column label="洗练石消耗数量" prop="refresh_balance"></el-table-column>
                <el-table-column label="萌宠属性" prop="egg_name"></el-table-column>
                <el-table-column label="孵化时间" prop="birth_time"></el-table-column>
                <el-table-column label="主人编号" prop="user_code"></el-table-column>
                <el-table-column label="主人昵称" prop="nickname"></el-table-column>
                <el-table-column label="操作" width="250px">
                    <template slot-scope="scope">
                        <el-button
                            size="mini"
                            type="text"
                            @click="handleResetUserNameTime(scope.row)"
                        >重置用户改名时间
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页 -->
            <cus-pagination
                v-show="total > 0"
                :limit.sync="listQuery.perPage"
                :page.sync="listQuery.page"
                :total="total"
                @pagination="getTableData"
            />
        </el-card>

    </div>
</template>

<script>
import { petUserList, userDeleteLock } from '@/api/petsManagement'

export default {
  name: 'petList',
  components: {},
  data() {
    return {
      loading: false,
      tableData: [],
      listQuery: {
        page: 1,
        perPage: 10
      },
      total: 1,
      options: []
    }
  },
  created() {
    this.getTableData()
  },
  methods: {
    indexMethod(index) {
      return this.total - (this.listQuery.page - 1) * this.listQuery.perPage - index
    },
    // 网络请求
    /**
     * getTableData
     * @desc 获取表格数据
     */
    async getTableData() {
      this.loading = true
      for (let key in this.listQuery) {
        this.listQuery[key] = this.listQuery[key] === '' ? undefined : this.listQuery[key]
      }
      const { data, code } = await petUserList(this.listQuery)
      if (code === 200) {
        this.tableData = data.list
        this.total = data.meta.total
      }
      this.loading = false

    },
    closeDialog(form) {
      if (this.$refs[form]) {
        this.$refs[form].clearValidate() // 清除整个表单的校验
        this.$refs[form].resetFields() // 清除整个表单的校验
      }
    },
    handleReset() {
      this.listQuery = {
        page: 1,
        perPage: 10
      }
      this.getTableData()
    },
    handleResetUserNameTime(row) {
        this.$confirm('确定重置用户改名时间', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            userDeleteLock({
                user_id: row.user_id,
            }).then(res => {
                if(res.code === 200) {
                    this.$message({
                        type: 'success',
                        message: '操作成功'
                    })
                }
                this.getTableData()
            })
        }).catch(() => {
            this.$message({
                type: 'info',
                message: '已取消删除'
            });
        });
    }
  }
}
</script>

<style lang="scss" scoped>
.infoBox {
  margin-top      : 15px;
  margin-bottom   : 15px;
  padding-bottom  : 15px;
  border          : 1px solid #999999;
  border-radius   : 10px;
  display         : flex;
  align-items     : baseline;
  justify-content : center;
  box-sizing      : border-box;
  padding-right   : 30px;

  .info_1 {
    display         : flex;
    justify-content : center;
    flex-direction  : column;
    width           : 100%;

    .info_2 {
      margin-top  : 15px;
      display     : flex;
      align-items : center;
      position    : relative;

      span {
        width      : 180px;
        text-align : right;
      }

      .tip {
        color     : red;
        font-size : 12px;
        position  : absolute;
        top       : 100%;
        left      : 120px;
      }
    }
  }
}

.item {
  width : 110px;
}

.formItemBox {
  display     : flex;
  align-items : center;
  flex-wrap   : wrap;

  width       : 100%;
  overflow    : hidden;
}

::v-deep {
  .formItemBox {
    .el-form-item {
      width : 50% !important;
    }
  }

}

</style>
